﻿using UnityEngine;
using System.Collections;

public enum LookDirection
{    
    center,
    up,
    left,
    down,
    right,
    mouse,
    circle,
    centerClosedMouth
}

public class EyeMovementScript : MonoBehaviour 
{
    public GameObject leftIris;
    public GameObject rightIris;
    public float verticalRadius;
    public float horizontalRadius;
    public float eyeSpeed;
    public LayerMask clickableLayer;

    private GameObject persistentManager;
    private Vector3 centerPointL;
    private Vector3 centerPointR;
    private Vector3 targetL;
    private Vector3 targetR;
    private Ray ray;
    private RaycastHit hit;
    private Vector3 hitTextCoord;

	void Start () 
    {
        centerPointL = leftIris.transform.localPosition;
        centerPointR = rightIris.transform.localPosition;
        LookAt(LookDirection.centerClosedMouth);
        persistentManager = GameObject.FindGameObjectWithTag("Manager");
        if (persistentManager.GetComponent<PersistentManagerScript>().GetState() == GameState.menu)
        {
            LookAt(LookDirection.mouse); 
        }
	}	

	void Update () 
    {       
          
	}

    private void SetNext()
    {       
         LookAt(LookDirection.circle); 
    }

    public void LookRandom()
    {
        LookAt((LookDirection)(Random.Range(0,5)));
    }

    public void LookAt(LookDirection dir)
    {
        StopCoroutine("LookAtIE");
        StartCoroutine("LookAtIE", dir);        
    }    

    private IEnumerator LookAtIE(LookDirection dir)
    {
        SetTargetPos(dir);
        int framesFromUpdate = 15;

        while (true)
        {
            if (dir == LookDirection.mouse || dir == LookDirection.circle)
            {
                if (framesFromUpdate >= 5)
                {
                    SetTargetPos(dir);
                    framesFromUpdate = 0;
                }

                else
                {
                    framesFromUpdate++;
                }
            }            

            leftIris.transform.localPosition= Vector3.MoveTowards(leftIris.transform.localPosition, targetL, eyeSpeed * Time.deltaTime);
            rightIris.transform.localPosition = Vector3.MoveTowards(rightIris.transform.localPosition, targetR, eyeSpeed * Time.deltaTime); 
            yield return null;
        }
    }

    private void SetTargetPos(LookDirection dir)
    {
       targetL = centerPointL;
       targetR = centerPointR;

        switch (dir)
        {
            case LookDirection.center:
                break;

            case LookDirection.up:
                targetL += new Vector3(0, verticalRadius, 0);
                targetR += new Vector3(0, verticalRadius, 0);
                break;

            case LookDirection.left:
                targetL += new Vector3(-horizontalRadius, 0, 0);
                targetR += new Vector3(-horizontalRadius, 0, 0);
                break;

            case LookDirection.down:
                targetL += new Vector3(0, -verticalRadius, 0);
                targetR += new Vector3(0, -verticalRadius, 0);
                break;

            case LookDirection.right:
                targetL += new Vector3(horizontalRadius, 0, 0);
                targetR += new Vector3(horizontalRadius, 0, 0);
                break;

            case LookDirection.mouse:

                UpdateRayCast();  
                float xPosL = 0;
                float xPosR = 0;              
                float yPosition = Mathf.Clamp(verticalRadius * (Input.mousePosition.y - Screen.height / 2) / (Screen.height / 2), -1, 1);

                if(hitTextCoord != new Vector3(-2, -2, -2))
                {
                    yPosition = Mathf.Clamp((hitTextCoord.y - 0.8f) * (Screen.height / 2) * 0.01f, -1, 1);
                    xPosL = Mathf.Clamp((Screen.width / 300) * (hitTextCoord.x - 0.3f), -1, 1);
                    xPosR = Mathf.Clamp((Screen.width / 300) * (hitTextCoord.x - 0.7f), -1, 1);  
                }

                targetL += new Vector3(horizontalRadius * xPosL,
                                       verticalRadius * yPosition,
                                       0);

                targetR += new Vector3(horizontalRadius * xPosR,
                                      verticalRadius * yPosition,
                                       0);
                break;

            case LookDirection.circle:
                targetL += new Vector3(horizontalRadius * Mathf.Sin(Time.timeSinceLevelLoad * 4), verticalRadius * Mathf.Cos(Time.timeSinceLevelLoad * 4), 0);
                targetR += new Vector3(horizontalRadius * Mathf.Sin(Time.timeSinceLevelLoad * 4), verticalRadius * Mathf.Cos(Time.timeSinceLevelLoad * 4), 0);
                break;

            case LookDirection.centerClosedMouth:
                break;

            default:
                break;
        }        
    }

    private void UpdateRayCast()
    {
        Ray ray = Camera.main.ScreenPointToRay(new Vector3(Input.mousePosition.x, Input.mousePosition.y));
        RaycastHit hit;

        if (Physics.Raycast(ray, out hit, 20.0f, clickableLayer))
        {
            if (hit.transform.tag == "Face")
            {
                hitTextCoord = hit.textureCoord;              
            }
        }

        else
        {
            hitTextCoord = new Vector3(-2, -2, -2);
        }
    }
}
